/*
 * @Author: hye0228 1261769443@qq.com
 * @Date: 2022-10-10 12:59:23
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2022-11-22 09:29:37
 * @FilePath: \v2.0.0\src\composables\useCURD.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ref } from "vue";
import { ElMessage } from "element-plus";

export default (options = {}) => {
  // 查询相关 ---------------------------------------------------------------
  const params = ref(getDefaultParams());

  const list = ref([]);

  const total = ref(0);

  const loading = ref(false);

  async function handleQuery() {
    const { queryApi } = options;
    loading.value = true;
    try {
      const res = await queryApi(params.value);
      loading.value = false;
      list.value = res.data.list || res.data;
      total.value = res.data.total;
    } catch {
      loading.value = false;
    }
  }

  async function handleReset() {
    params.value = getDefaultParams();
    handleQuery();
  }

  function getDefaultParams() {
    const { newParams } = options;
    return Object.assign({ page: 1, limit: 10 }, newParams);
  }

  // 新增/编辑相关 -----------------------------------------------------------------
  async function handleCreate(data) {
    const { createApi } = options;
    loading.value = true;
    try {
      const { message } = await createApi(data);
      dialogVisible.value = false;
      loading.value = false;
      ElMessage({ type: "success", message });
      handleQuery();
    } catch {
      loading.value = false;
      ElMessage({ type: "error", message: "操作失败" });
    }
  }

  const formData = ref({});
  async function getOne(id) {
    const { getOneApi } = options;
    const res = await getOneApi(id);
    formData.value = res.data;
  }

  const dialogVisible = ref(false);
  const dialogTitle = ref("");
  function openDialog(id) {
    dialogTitle.value = id ? "编辑" : "新增";
    if (id) {
      getOne(id);
    }
    dialogVisible.value = true;
  }

  async function handleUpdate(id, data) {
    const { updateApi } = options;
    loading.value = true;
    try {
      const { message } = await updateApi(id, data);
      dialogVisible.value = false;
      loading.value = false;
      ElMessage({ type: "success", message });
      handleQuery();
    } catch {
      loading.value = false;
      ElMessage({ type: "error", message: "操作失败" });
    }
  }

  function handleConfirm(data) {
    if (data.id) {
      handleUpdate(data.id, data);
    } else {
      handleCreate(data);
    }
  }
  // 删除相关 -----------------------------------------------------------------
  async function handleDelete(id) {
    const { deleteApi } = options;
    loading.value = true;
    try {
      const res = await deleteApi(id);
      loading.value = false;
      handleQuery();
      return res;
    } catch {
      loading.value = false;
    }
  }

  return {
    params,
    handleQuery,
    list,
    loading,
    handleReset,
    handleCreate,
    openDialog,
    dialogVisible,
    handleDelete,
    formData,
    handleUpdate,
    dialogTitle,
    total,
    handleConfirm,
  };
};
